using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Administration;


namespace ACM.cmsarea08.Administration.Controls {
	public partial class OptMenuItemChooser : ACM.cmsarea08.Administration.Core.OptPageBase {
		protected HtmlGenericControl controlli;
		
		const string input_radio ="<input type=\"radio\" value=\"{0}\" name=\"menu_items\" id=\"mitem{0}\" />";
		const string input_label ="<label for=\"mitem{0}\" lang=\"{1}\" >{2}</label>";
		const string input_span ="<br class=\"clearboth\" /><span >{0}</span>";
		
		private DataSet DsMenuItem {
			get {
				if(ViewState[this.ID + "menuitem"]==null)
					return null;
				else
					return (DataSet)ViewState[this.ID + "menuitem"];
			}
			set {
				ViewState[this.ID + "menuitem"] = value;
			}
		}
		
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
	
		private void Page_Load(object o , EventArgs e) {
			InitMenuItems();
			
		}
		
		private void InitMenuItems() {
			if(DsMenuItem==null) {
				//leggi da DB;
				ACM.cmsarea08.Administration.Core.DBClass dclass = new ACM.cmsarea08.Administration.Core.DBClass();
				DsMenuItem = dclass.getAdminMenuItems();
			}
			if(DsMenuItem==null) 
				return;
			
			//altrimenti riempi:
			controlli.Controls.Clear();
			HtmlTable table = new HtmlTable();
			int counter = 0;
			HtmlTableRow tr = new HtmlTableRow();
			HtmlTableCell td0;
			HtmlTableCell td1;
			
			foreach(DataRow dr in DsMenuItem.Tables[0].Rows) {

				if(counter>1) {
					table.Controls.Add(tr);
					counter=0;
					tr = new HtmlTableRow();
				}
				if(counter==0) {
					td0 = new HtmlTableCell();
					td0.Controls.Add(new LiteralControl(
						string.Format(input_radio,dr["id"]) + 
						string.Format(input_label,dr["id"],dr["lingua"],dr["titolo"]) + 
						string.Format(input_span,dr["descrizione"])
					));
					tr.Controls.Add(td0);
					counter++;                 
				} else {
					td1 = new HtmlTableCell();
					td1.Controls.Add(new LiteralControl(
						string.Format(input_radio,dr["id"]) + 
						string.Format(input_label,dr["id"],dr["lingua"],dr["titolo"])+ 
						string.Format(input_span,dr["descrizione"])
					));
					tr.Controls.Add(td1);
					counter++;  					
				}
			}
			if(tr.Controls.Count>0)
				table.Controls.Add(tr);
			controlli.Controls.Add(table);
		}
		
	}
	
}
